<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use App\Models\UserModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class WithdrawController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/withdraw/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){
        // DB::connection()->enableQueryLog(); // 開啟查詢日誌
        if(isset($_GET['keywords'])){
            $keywords = trim($_GET['keywords']);
            // $keywords = '使用';
            $list = DB::table(DB::raw('zzb_withdraw w'))->select(DB::raw('w.id,w.uid,u.user_name,u.nickname,w.money,w.status,w.add_time,w.time_end,m.account_money'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('w.uid'))->leftJoin(DB::raw('zzb_user_money m'), DB::raw('m.uid'), '=', DB::raw('w.uid'))->where(DB::raw('u.user_name'),'like','%'.$keywords.'%')->orderBy(DB::raw('w.id'),'desc')->paginate(15);
        }else{
            $list = DB::table(DB::raw('zzb_withdraw w'))->select(DB::raw('w.id,w.uid,u.user_name,u.nickname,w.money,w.status,w.add_time,w.time_end,m.account_money'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('w.uid'))->leftJoin(DB::raw('zzb_user_money m'), DB::raw('m.uid'), '=', DB::raw('w.uid'))->orderBy(DB::raw('w.id'),'desc')->paginate(15);
        }
        $withdraw_status = config('app.withdraw_status');
        
        return view('backstage/withdraw/index',['list'=>$list,'withdraw_status'=>$withdraw_status]);
    }
    public function edit($id){
        $table = 'withdraw';
        $data =['id'=>$id];
        $withdraw = CommonModel::getOne($table,$data);
        $withdraw_status = config('app.withdraw_status');
        // dd($withdraw->uid);
        $user = UserModel::getOneUser(['id'=>$withdraw->uid]);
        // dd($user);
        return view('backstage/withdraw/edit',['withdraw'=>$withdraw,'user'=>$user,'withdraw_status'=>$withdraw_status]);
    }
    public function doedit(Request $request){
        if($request->isMethod('POST')){
            $data = $request->all();
            $id = $data['id'];
            $withdraw['status'] = intval($data['status']);
            $withdraw_status = config('app.withdraw_status');
            $withdrawInfo = CommonModel::getOne('withdraw',['id'=>$id]);
            $save = CommonModel::updateData('withdraw',['id'=>$id],$withdraw);
            if($save == true){
                $logContent = "将提现状态由:<b style='color:red'>".$withdraw_status[$withdrawInfo->status]."</b>,修改为:<b style='color:red'>".$withdraw_status[$withdraw['status']]."</b>。";
                saveCommonLog($id, $logContent, 'withdraw', session('admin')->real_name, session('admin')->id, 7);
                if($withdraw['status']==1){
                    userMoneyLog($withdrawInfo->uid, 3, $withdrawInfo->money, $data['id'],'用戶提現成功');
                    // 完成時間
                    CommonModel::updateData('withdraw',['id'=>$id],['time_end'=>time() ]);
                }
                $arrJson = array('status' => 1, 'message'=>'操作成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'操作失敗');
            }
            return json_encode($arrJson);
        }
    }


}
